/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace UserNotifier
{
    public class ConsoleWarnModule : WarnModule
    {
        private UdpClient m_Socket = new UdpClient();
        //private UdpClient()
        private int m_Port = 27005;
        //private string m_WarnString = "Error";

        public int ClientPort
        {
            get { return m_Port; }
            set { m_Port = value; }
        }
        public ConsoleWarnModule(int interval, int port)
            :base(interval)
        {
            m_Port = port;
        }

        public ConsoleWarnModule()
            : base(5000)
        {
            //m_MsgData = new byte[m_MsgHeader.Length + m_MsgFooter.Length];
            //m_MsgHeader.CopyTo(data, 0);
        }

        public override string Description
        {
            get
            {
                return Info.ConsoleDesc;
            }
        }

        public override WarnModuleTypes Type
        {
            get { return WarnModuleTypes.ConsoleWarnModule; }
        }

        /*protected override bool InitDelegate()
        {
            //m_Socket.Send()
            //m_Socket.Connect("localhost", m_Port);
            //m_Socket.Connect()
            m_Socket.Connect("localhost", m_Port);
            return true;
        }*/

        private byte[] m_MsgData = new byte[0];
        private static byte[] m_MsgHeader = new byte[] { 255, 255, 255, 255, (byte)'y', (byte)'\n', (byte)'\n', (byte)'\n', (byte)'\n' };
        private static byte[] m_MsgFooter = new byte[] { (byte)'\n', (byte)'\n', (byte)'\n' };

        protected override void Warn()
        {
            if (m_MsgData.Length > 0 && m_Socket != null)
            {
                m_Socket.Send(m_MsgData, m_MsgData.Length, "localhost", m_Port);//, "localhost", m_Port);
                //Monitor.Exit(m_MsgData);
            }
        }

        protected override void EventsChanged(WarnEvents newActiveEvents)
        {
            StringBuilder b = new StringBuilder();
            if ((newActiveEvents & WarnEvents.NoAequitas) != WarnEvents.None)
            {
                b.AppendLine("No aequitas running!");
            }
            if ((newActiveEvents & WarnEvents.NoReplay) != WarnEvents.None)
            {
                b.AppendLine("No demo recording!");
            }
            if ((newActiveEvents & WarnEvents.TakeStatus) != WarnEvents.None)
            {
                b.AppendLine("Take status-screenshot!");
            }
            if ((newActiveEvents & WarnEvents.RestartReplay) != WarnEvents.None)
            {
                b.AppendLine("Teams changed, record new demo?");
            }
            string m_WarnString = b.ToString().Trim();
            if (!string.IsNullOrEmpty(m_WarnString))
            {
                ASCIIEncoding a = new ASCIIEncoding();
                //Monitor.Enter(m_MsgData);
                m_MsgData = new byte[a.GetByteCount(m_WarnString) + m_MsgHeader.Length + m_MsgFooter.Length];
                m_MsgHeader.CopyTo(m_MsgData, 0);
                a.GetBytes(m_WarnString, 0, m_WarnString.Length, m_MsgData, m_MsgHeader.Length);
                m_MsgFooter.CopyTo(m_MsgData, m_MsgData.Length - m_MsgFooter.Length);
                //Monitor.Exit(m_MsgData);
            }
            else
            {
                //Monitor.Enter(m_MsgData);
                m_MsgData = new byte[0];
                //Monitor.Exit(m_MsgData);
            }

            base.EventsChanged(newActiveEvents);
        }

        public override void Test()
        {
            throw new NotSupportedException(Info.TestingNotSupported);
        }

        protected override void CleanupDelegate()
        {
            //m_Socket.Close();
        }
    }
}
